<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Experience extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(1);
		$this->load->model('Candidate_experience_model', 'experience');
		$this->lang->load('candidate/experience', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$experience_list = $this->experience->get(array('idcandidate' => $this->session->userdata['idprofile']));
				
		$grid_experience = array();
        
        foreach ($experience_list->result() as $row)
		{
			$grid_experience[] = '<div class="row"><span><span class="gtitle">' . $row->title . ' ('.$row->hierarchy.')</span>'
			. '<br/><span class="gdesc">' . $row->specialty . ' ('.$row->category.')<br/>'.$row->company.' - ' . format_date($row->startdate, true)
			. ($row->finishdate ? ' &agrave; ' . format_date($row->finishdate, true) : ($row->idnow == 'Y' ? (' ' . $this->lang->line('experience_sofar')) : ''))
			.'<br/>'.($row->statename != '' ? $row->statename.' / ' : '') . $row->countryname.'<br/><br/>' 
			. $row->description . '</span></span>
			<div class="gtool"><a onclick="CandidateExperience.editExperience('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidateExperience.deleteExperience('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_experience) {
			 $data['grid_experience'] = '<center>'.$this->lang->line('experience_noresults').'</center>';
		} else {
			 $data['grid_experience'] = implode('<hr/>', $grid_experience);
		}
		
		exitHTML($data['grid_experience']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('idhierarchy', 'idcategory', 'idspecialty', 'company')))
		{
			formError($this->lang->line('experience_missingfield'), true);
		}
		
		if ($data['startdate'] != '' && !valid_date($data['startdate']))
		{
			formError($this->lang->line('experience_invalidstartdate'), true);
		} else {
			$data['startdate'] = format_date($data['startdate']);
		}
		
		if (isset($data['finishdate']) && $data['finishdate'] != '' && !valid_date($data['finishdate']))
		{
			formError($this->lang->line('experience_invalidfinishdate'), true);
		} else {
			$data['finishdate'] = format_date($data['finishdate']);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		$data['idnow'] = isset($data['idnow']) && $data['idnow'] == 'Y' ? 'Y' : 'N';
		
		if ($this->experience->get(array('idhierarchy' => $data['idhierarchy'], 'idcategory' => $data['idcategory'],
									'idspecialty' => $data['idspecialty'], 'company' => $data['company'], 'startdate' => $data['startdate']))->row()) {
			formError($this->lang->line('experience_experienceexists'), true);
		}
		
		if ($this->experience->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateExperience.getExperienceGrid();closeBox();');
		}
	}
	
	public function update($id_experience = '')
	{
		$data = $_POST;
		
		if (missing_field($data, array('idhierarchy', 'idcategory', 'idspecialty', 'company')))
		{
			formError($this->lang->line('experience_missingfield'), true);
		}
		
		if ($data['startdate'] != '' && !valid_date($data['startdate']))
		{
			formError($this->lang->line('experience_invalidstartdate'), true);
		} else {
			$data['startdate'] = format_date($data['startdate']);
		}
		
		if (isset($data['finishdate']) && $data['finishdate'] != '' && !valid_date($data['finishdate']))
		{
			formError($this->lang->line('experience_invalidfinishdate'), true);
		} else {
			$data['finishdate'] = format_date($data['finishdate']);
		}
		
		$data['idnow'] = isset($data['idnow']) && $data['idnow'] == 'Y' ? 'Y' : 'N';
		
		if ($this->experience->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateExperience.getExperienceGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('experience_missingfield'));
		}
		
		if ($this->experience->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidateExperience.getExperienceGrid();');
		}
	}
	
	public function edit($id_experience = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_experience'] = $id_experience;
		
		$data['startdate'] = '';
		$data['finishdate'] = '';
		$data['title'] = '';
		$data['company'] = '';
		$data['description'] = '';
		
		if (is_numeric($id_experience))
		{
			$experience_data = $this->experience->get(array('idexperience' => $id_experience))->row();
			
			$data['title'] = $experience_data->title;
			$data['company'] = $experience_data->company;
			$data['description'] = $experience_data->description;
			$data['startdate'] = format_date($experience_data->startdate, true);
			$data['finishdate'] = format_date($experience_data->finishdate, true);
			$data['extrascript']  = '$("#idhierarchy").val("'.$experience_data->idhierarchy.'");';
			$data['set_category_value'] = '$("#idcategory").val("'.$experience_data->idcategory.'");';
			$data['extrascript'] .= '$("#idspecialty").val("'.$experience_data->idspecialty.'");';
			if ($experience_data->idnow == 'Y') {
				$data['extrascript'] .= '$("#idnow").attr("checked", true);';
			}
			$data['extrascript'] .= '$("#idcountry").val("'.$experience_data->idcountry.'");';
			$data['extrascript'] .= '$("#idstate").val("'.$experience_data->idstate.'");';
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('candidate/experience', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>